<?php
// +----------------------------------------------------------------------
// | astp [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Author 荒年 < 931274989@qq.com >
// +----------------------------------------------------------------------
// | Explain 发起HTTP/HTTPS请求
// +----------------------------------------------------------------------
namespace app\yunmeng\service;

use app\yunmeng\model\YmCarlog as YmCasrlogModel;
use app\yunmeng\service\HttpRequest;
use app\yunmeng\service\Accesstoken;
use think\facade\Request;
use think\facade\Config;

class Jssdk
{
  public function getticket()
  {
    $http = new HttpRequest();
    $access = new Accesstoken();
    // 获取签名
    $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token='.$access->getAccessToken();;
    $json = $http->http_request($url);
    $arr = json_decode($json,true);
    return $arr['ticket'];
  }

  public function signature()
  {
    $ticket = $this->getticket();
    $noncestr = getRandChar(16);
    $time = time();
    $url = Request::url(true);
    $str = 'jsapi_ticket=%s&noncestr=%s&timestamp=%s&url=%s';
    $str = sprintf($str,$ticket,$noncestr,$time,$url);
    $signature = sha1($str);
    $data = [
      'appid' => Config::get('wewhat.APPID'),
      'ticket' => $ticket,
      'noncestr' => $noncestr,
      'time' => $time,
      'url' => $url,
      'signature' => $signature
    ];
    $json = json_encode($data);
    return $json;
  }
}
?>